<?php
/*
	业务员中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
//$this->model('member')->set_parent(90,1);
//$this->model('member')->min_agent_level();
//$this->model('member')->upyao_fee();
$member=$this->model('member')->checklogin();
if($_RQ['dian_mid']){
	$this->checklogin();
	$data=array(
		'dian_mid'=>intval($_RQ['dian_mid']),
		'agent_id'=>intval($_RQ['dian_mid']),
		'is_agent'=>1,
		'agenttime'=>TIMESTAMP,
	);
	if($member['dian_mid']){
		exi('您已经是业务员了！','error','/index/');
	}
	if($member['mid']==$data['dian_mid']){
		exi('不能成为自己的业务员！','error','/index/');
	}
	if($member['level_id']){
		exi('您是经销商，不能成为业务员！','error','/index/');
	}
	$this->update('member',$data,array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	exi('成功成为业务员！','','/yewu/');
}
$member=$this->model('member')->checkmember();
if(!$member['dian_mid']){
	exi('您还不是业务员！请联系经销商添加！','error','/index/');
}
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('page_setting');
	set_html('title',$return['setting']['title']?:'业务员中心');
	$basic=model('core/member')->basic($_SESSION['uid']);
	$member=array_merge($member,$basic);
	$member['agent']=model('core/member')->basic($member['dian_mid']);
	if($member['agent']['level_id']){
		$member['level']=$this->getcolumn('level',array('id'=>$member['agent']['level_id']),'title');
	}else{
		$member['level']=$return['setting']['demolevel'];
	}
	if($member['t_time'] >0){
    	$member['b_time'] = date('Y-m-d',$member['t_time']);
	}else{
	    $member['b_time'] = 0;
	}
	$member['url']=custom_url('/index/getbufee/sale_id='.$_SESSION['uid'],true);
	$dian=$this->get('member',array('mid'=>$member['dian_mid']));
	$dian['downdata']=json_decode($dian['downdata'],true);
	$return['bufee']=$dian['downdata']['bufee'];
	$return['member']=$member;
	$data=array();
	//已结算佣金
	$data['balance_total']=$this->sum('balance',array('mid'=>$_SESSION['uid']),'money');
	$data['balance_num']=$this->count('balance',array('mid'=>$_SESSION['uid']));
	//待结算佣金
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1);
	/*if($this->setting['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($this->setting['balance_day'])*86400;
		$params['complete_time <']=$time;
	}*/
	$data['commission_1']=$this->sum('commission',$params,'money');
	$data['commission_total']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'money');
	$data['commission_area']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'level'=>16),'money');
	$data['commission_xuni']=$this->sum('commission',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'level'=>18),'money');
	$data['saleorderfee']=$this->getsum('order',array('sale_id'=>$_SESSION['uid']),'order_price');
	$data['saleordernum']=$this->count('order',array('sale_id'=>$_SESSION['uid']),'id');
	$levels=array();
	$agent_ids=$_SESSION['uid'];
	$allagentnum=0;
	for($i=0;$i<$this->setting['level'];$i++){
		$level=$i+1;
		if(!$agent_ids){
			$levels[$level]=0;
			continue;
		}
		$agent_ids=$this->getall('member',array('agent_id'=>$agent_ids),array('mid'),'mid');
		if(!$agent_ids){
			$levels[$level]=0;
		}else{
			$agent_ids=array_keys($agent_ids);
			$levels[$level]=count($agent_ids);
			$allagentnum+=$levels[$level];
		}
	}
	$return['game_wait']=model('jy_weishop/task')->pop_game('',8);
	$return['member']['member_num']=$allagentnum;
	$return['data']=$data;
	$return['xuniname']=$this->xuniname;
	$return['rank']=$this->plugin_setting('rank');
	exi($return);
}
if($_RQ['op']=='apply'){
	$apply=$this->get('apply',array('mid'=>$_SESSION['uid']));
	if($apply['status']==0 && $apply['id']){
		exi('您已申请，请等待审核！','error');
	}
		$post=get_postdata();
		if(!$post['agree']){
			exi('请先阅读协议！','error');
		}
		if(!$post['realname']){
			exi('请输入姓名！','error');
		}
		if(!$post['mobile']){
			exi('请输入联系电话！','error');
		}
		if($post['diyformfields']){
			foreach ($post['diyformfields'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
		$member_update=array(
			'realname'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'diyformdata'=>ijson_encode($post['diyformfields']),
		);
		if($post['wechat']){
			$member_update['wechat']=trim($post['wechat']);
		}
		$this->model('member')->member_update($_SESSION['uid'],$member_update);	
		if($post['agent_id']){
			$agent_id=intval($post['agent_id']);
			if($agent_id){
				$this->model('member')->set_parent($_SESSION['uid'],$agent_id);
			}
		}
		$this->model('member')->apply_agent($_SESSION['uid']);
		exi('申请成功！请耐心等待管理员审核！','',custom_url('jy_weishop/index/',true));
}
?>